package com.bacon.gmall.web.controller;

import com.bacon.gmall.list.client.ListFeignClient;
import com.bacon.gmall.model.list.SearchParam;
import com.bacon.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author： bacon、
 * @version： 1.0
 * @create： 2020-12-30 16:40
 * @description:
 */
@Controller
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;

    @GetMapping("/list.html")
    public String list(SearchParam searchParam, Model model) {

        /**
         * 1、搜索框回显
         * 2、选中项回显
         * 3、品牌集合
         * 4、平台属性值集合
         * 5、排序
         * 6、商品集合
         * 7、分页
         * 8、基础URL（每加一次条件，将条件拼接到后面）
         */

        //1搜索框回显
        model.addAttribute("searchParam", searchParam);

        //2选中项回显
        //品牌
        String trademarkParam = buildertrademark(searchParam);
        model.addAttribute("trademarkParam", trademarkParam);
        //属性值
        List<Map> porpsParamList = builderporpsParamList(searchParam);
        model.addAttribute("propsParamList", porpsParamList);

        //3品牌集合
        SearchResponseVo list = listFeignClient.list(searchParam);
        model.addAttribute("trademarkList", list.getTrademarkList());

        //4平台属性值集合
        model.addAttribute("attrsList", list.getAttrsList());

        //5排序
        Map orderMap = builderOrderMap(searchParam);
        model.addAttribute("orderMap", orderMap);

        //6商品集合
        model.addAttribute("goodsList", list.getGoodsList());

        //7分页
        model.addAttribute("pageNo", list.getPageNo());
        model.addAttribute("totalPages", (list.getTotalPages() - 1 + list.getPageSize()) / list.getPageSize());

        //8基础URL（每加一次条件，将条件拼接到后面）
        String urlParam = builderUrlParam(searchParam);
        model.addAttribute("urlParam", urlParam);
        return "list/index";
    }

    private Map builderOrderMap(SearchParam searchParam) {
        Map map = new HashMap();

        String order = searchParam.getOrder();
        if (!StringUtils.isEmpty(order)) {
            String[] split = order.split(":");
            map.put("type", split[0]);
            map.put("sort", split[1]);
        } else {
            map.put("type", "1");
            map.put("sort", "desc");
        }
        return map;
    }

    private List<Map> builderporpsParamList(SearchParam searchParam) {
        List<Map> listMap = new ArrayList<>();
        String[] props = searchParam.getProps();
        if (null != props && props.length > 0) {
            for (String prop : props) {
                Map map = new HashMap();

                //prop  平台Id : 平台值 ： 平台属性名
                String[] split = prop.split(":");
                map.put("attrName", split[2]);
                map.put("attrValue", split[1]);
                map.put("attrId", split[0]);
                listMap.add(map);
            }
        }
        return listMap;
    }

    private String buildertrademark(SearchParam searchParam) {
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)) {

            String[] split = trademark.split(":");
            return "品牌:" + split[1];
        }
        return null;
    }

    //8 拼接urlParam
    private String builderUrlParam(SearchParam searchParam) {

        StringBuilder sb = new StringBuilder();
        sb.append("/list.html?");

        if (!StringUtils.isEmpty(searchParam.getKeyword())) {
            sb.append("&keyword=").append(searchParam.getKeyword());
        }

        if (!StringUtils.isEmpty(searchParam.getTrademark())) {
            sb.append("&trademark=").append(searchParam.getTrademark());
        }

        String[] props = searchParam.getProps();
        if (null != props && props.length > 0) {
            for (String prop : props) {
                sb.append("&props=").append(prop);
            }
        }
        return sb.toString();
    }
}
